﻿Imports System.Data.OleDb

Public Class Edit_UserProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim conn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("carsconn").ConnectionString)
            Dim SqlString As String = "SELECT * FROM User_Profile WHERE U_Name=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                dr.Read()

                If Not IsDBNull(dr("U_DOB")) Then txtDOB.Text = dr("U_DOB")
                If Not IsDBNull(dr("U_Email")) Then txtEmail.Text = dr("U_Email")
                If Not IsDBNull(dr("U_Desc")) Then txtDesc.Text = dr("U_Desc")
                If Not IsDBNull(dr("U_Country")) Then txtCountry.Text = dr("U_Country")

            End If
        End If
    End Sub

    Protected Sub btnSaveProfile_Click(sender As Object, e As EventArgs) Handles btnSaveProfile.Click
        Dim newFileName As String = ""
        Dim SqlString As String
        If img_Upload.HasFile Then
            Dim myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() & ".jpg"
            Dim fileLocationOnServerHardDisk = Request.MapPath("Images/ProfilePic") & "/" & newFileName
            img_Upload.SaveAs(fileLocationOnServerHardDisk)
        End If
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("carsconn").ConnectionString)
        If img_Upload.HasFile Then
            SqlString = "UPDATE User_Profile SET U_DOB=@f1,U_Email=@f2,U_Desc=@f3,U_Country=@f4,U_Image=@f5 WHERE U_Name=@f6"
        Else
            SqlString = "UPDATE User_Profile SET U_DOB=@f1,U_Email=@f2,U_Desc=@f3,U_Country=@f4 WHERE U_Name=@f6"
        End If


        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text


        cmd.Parameters.AddWithValue("@f1", txtDOB.Text)
        cmd.Parameters.AddWithValue("@f2", txtEmail.Text)
        cmd.Parameters.AddWithValue("@f3", txtDesc.Text)
        cmd.Parameters.AddWithValue("@f4", txtCountry.Text)
        If img_Upload.HasFile Then cmd.Parameters.AddWithValue("@f5", "Images/ProfilePic/" & newFileName)
        cmd.Parameters.AddWithValue("@f6", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()
        Response.Redirect("ViewProfile.aspx?U_Name=" & User.Identity.Name)
    End Sub
End Class